using UnityEngine;
namespace M13.Game.Entity.Player
{
    public class PlayerStateAim : PlayerStateBase
    {
        private float _lateralMovementSpeed;
        private float _forwardMovementSpeed;
        private float _backwardMoveMentSpeed;

        private Rigidbody _rigidbody;

        private Vector3 _input;
        private Vector3 _mouseTarget;
        private Vector3 _skewedTargetInput;
        private Vector3 _skewedCameraInput;
        private Vector3 _moveVectorOnAim;
        public override void Enter()
        {
            _rigidbody = player.GetComponent<Rigidbody>();
            //_lateralMovementSpeed = _player.lateralMovementSpeed;
            //_forwardMovementSpeed = _player.forwardMovementSpeed;
            //_backwardMoveMentSpeed = _player.backwardMoveMentSpeed;
            animator.Play("PlayerStateAim", 0);
        }
        public override void Update()
        {
            //_input = new Vector3(_inputCtrlSystem.AXIS_X_INPUT, 0, _inputCtrlSystem.AXIS_Y_INPUT);
            //_mouseTarget = _player.mouseTarget;
            ////������任
            //Matrix4x4 matrix1 = Matrix4x4.Rotate(Quaternion.Euler(0, _camera.rotation.eulerAngles.y, 0));
            //_skewedCameraInput = matrix1.MultiplyPoint3x4(_input).normalized;
            ////��Ŀ�귽��任
            ////Matrix4x4 matrix2 = Matrix4x4.Rotate(Quaternion.LookRotation(
            ////    new Vector3((_mouseTarget - _player.transform.position).x, 0f, (_mouseTarget - _player.transform.position).z).normalized,Vector3.up));
            ////_skewedTargetInput = matrix2.MultiplyPoint3x4(_skewedCameraInput).normalized;
            //Transform aimTrans = _player.transform;
            //_skewedTargetInput = aimTrans.transform.InverseTransformDirection(_skewedCameraInput);

            ////��ɫ����
            //_player.transform.LookAt(new Vector3(_mouseTarget.x, 0f, _mouseTarget.z), Vector3.up);
            ////��ɫ�ƶ�����
            //_animator.SetFloat("SpeedY", _skewedTargetInput.z);
            //_animator.SetFloat("SpeedX", _skewedTargetInput.x);

            //_moveVectorOnAim = new Vector3(_skewedCameraInput.x * _lateralMovementSpeed, 0f, _skewedCameraInput.z * _forwardMovementSpeed);

            //Debug.Log(new Vector3((_mouseTarget - _player.transform.position).x, 0f, (_mouseTarget - _player.transform.position).z).normalized);
            //if (_inputCtrlSystem.EXECUTE_1_INPUT == 0f && _input == Vector3.zero) _stateMachine.SwitchState(typeof(PlayerStateIdle));
            //if (_inputCtrlSystem.EXECUTE_1_INPUT == 0f && _input != Vector3.zero) _stateMachine.SwitchState(typeof(PlayerStateMove));
        }
        public override void FixedUpdate()
        {
            _rigidbody.MovePosition(player.transform.position + _moveVectorOnAim * Time.deltaTime);
        }
        public override void Exit()
        {
            _skewedTargetInput = Vector3.zero;
            animator.SetFloat("SpeedY", _skewedTargetInput.z);
            animator.SetFloat("SpeedX", _skewedTargetInput.x);
        }
        public override bool TryEnter()
        {
            return true;
        }
        public override bool TryExit()
        {
            return true;
        }
        public override void OnDrawGizmos()
        {
            Gizmos.color = Color.green;
            Gizmos.DrawLine(player.transform.position + new Vector3(0, 1.2f, 0),
                new Vector3(player.transform.position.x, 1.2f, player.transform.position.z) + _skewedTargetInput);
            Gizmos.color = Color.red;
            Gizmos.DrawLine(player.transform.position + new Vector3(0, 1.2f, 0),
                new Vector3(player.transform.position.x, 1.2f, player.transform.position.z) + _skewedCameraInput);
            Gizmos.color = Color.blue;
            Gizmos.DrawLine(player.transform.position + new Vector3(0, 1.2f, 0),
                new Vector3(player.transform.position.x, 1.2f, player.transform.position.z)
                + new Vector3((_mouseTarget - player.transform.position).x, 0f, (_mouseTarget - player.transform.position).z).normalized);
        }
    }
}